package com.example.admin.controller;

import com.example.admin.common.result.R;
import com.example.admin.entity.SysMenu;
import com.example.admin.service.MenuService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Api(tags = "菜单管理接口")
@RestController
@RequestMapping("/api/menu")
public class MenuController {

    @Autowired
    private MenuService menuService;

    /**
     * 获取菜单树形结构
     */
    @ApiOperation("获取菜单树形结构")
    @GetMapping("/tree")
    public R<List<SysMenu>> getMenuTree() {
        return R.ok(menuService.getMenuTree());
    }

    /**
     * 根据ID获取菜单详情
     */
    @ApiOperation("根据ID获取菜单详情")
    @GetMapping("/get")
    public R<SysMenu> getById(@RequestParam Long id) {
        return R.ok(menuService.getById(id));
    }


    @ApiOperation("新增菜单")
    @PostMapping("/add")
    public R<Void> addMenu(@RequestBody @ApiParam("菜单信息") SysMenu menu) {
        menuService.addMenu(menu);
        return R.ok();
    }

    @ApiOperation("更新菜单")
    @PostMapping("/update")
    public R<Void> updateMenu(@RequestBody @ApiParam("菜单信息") SysMenu menu) {
        menuService.updateMenu(menu);
        return R.ok();
    }

    @ApiOperation("删除菜单")
    @GetMapping("/delete")
    public R<Void> deleteMenu(@ApiParam("菜单ID") @RequestParam Long id) {
        menuService.deleteMenu(id);
        return R.ok();
    }

    /**
     * 获取角色的菜单ID列表
     */
    @ApiOperation("获取角色的菜单ID列表")
    @GetMapping("/role/{roleId}")
    public R<List<Long>> getRoleMenuIds(@PathVariable Long roleId) {
        return R.ok(menuService.getRoleMenuIds(roleId));
    }

    /**
     * 更新角色菜单权限
     */
    @ApiOperation("更新角色菜单权限")
    @PostMapping("/role/{roleId}")
    public R<Void> updateRoleMenus(
        @PathVariable Long roleId,
        @RequestBody List<Long> menuIds
    ) {
        menuService.updateRoleMenus(roleId, menuIds);
        return R.ok();
    }

    /**
     * 获取菜单下拉选项
     */
    @ApiOperation("获取菜单下拉选项")
    @GetMapping("/options")
    public R<List<SysMenu>> getMenuOptions() {
        return R.ok(menuService.getMenuOptions());
    }
} 